package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.config.MyConfig;
import com.neusoft.dto.EmpLoginADto;
import com.neusoft.dto.EmpLoginEDto;
import com.neusoft.exception.EmpExistException;
import com.neusoft.mapper.EmpMapper;
import com.neusoft.model.Emp;
import com.neusoft.service.LoginService;
import com.neusoft.utils.EmpUtil;
import com.neusoft.utils.StringUtil;
import com.neusoft.utils.TokenUtil;
import com.neusoft.vo.EmpLoginVo;
import com.neusoft.vo.EmpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * @Author 王柯淋
 * @Date 2025/1/8 17:17
 * @DESCRIBER
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    EmpMapper empMapper;
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    MyConfig myConfig;

    @Autowired
    JavaMailSender javaMailSender;

    @Override
    public EmpLoginVo EmailLogin(EmpLoginEDto empLoginEDto) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",empLoginEDto.getEmpEmail());
        Emp emp = empMapper.selectOne(queryWrapper);
        if (emp == null){
            System.out.println("邮箱不存在");
            throw new EmpExistException(7000,"邮箱不存在");
        }else {
            String empPassword = emp.getPassword();
            if(
                empLoginEDto.getPassword().equals(empPassword)
            ){
                String token = tokenUtil.toToken(emp.getId().toString());
                EmpLoginVo empLoginVo = new EmpLoginVo();
                empLoginVo.setId(emp.getId());
                empLoginVo.setEmail(emp.getEmail());
                empLoginVo.setToken(token);
                empLoginVo.setAccount(emp.getAccount());
                empLoginVo.setEmpname(emp.getEmpname());
                empLoginVo.setAuthority_id(emp.getAuthorityId());
                return empLoginVo;
            }else{
                throw new EmpExistException(7000,"password error");
            }
        }
    }

    @Override
    public EmpLoginVo AccountLogin(EmpLoginADto empLoginADto) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",empLoginADto.getEmpAccount());
        Emp emp = empMapper.selectOne(queryWrapper);
        if (emp == null){
            System.out.println("账号不存在");
            throw new EmpExistException(7000,"账号不存在");
        }else {
            String empPassword = emp.getPassword();
            if(
                    empLoginADto.getPassword().equals(empPassword)
            ){
                String token = tokenUtil.toToken(emp.getId().toString());
                EmpLoginVo empLoginVo = new EmpLoginVo();
                empLoginVo.setId(emp.getId());
                empLoginVo.setEmail(emp.getEmail());
                empLoginVo.setToken(token);
                empLoginVo.setAccount(emp.getAccount());
                empLoginVo.setEmpname(emp.getEmpname());
                empLoginVo.setAuthority_id(emp.getAuthorityId());
                return empLoginVo;
            }else{
                throw new EmpExistException(7000,"password error");
            }
        }
    }

    @Override
    public String ReSetPassword(EmpLoginEDto empLoginEDto) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",empLoginEDto.getEmpEmail());
        Emp emp = empMapper.selectOne(queryWrapper);
        if (emp == null){
            System.out.println("邮箱不存在");
            throw new EmpExistException(7000,"邮箱不存在");
        }else {
            //更新密码为随机密码
            emp.setPassword(StringUtil.getRandomPassword());
            empMapper.updatePasswordByEmail(emp.getEmail(),emp.getPassword());
            return emp.getPassword();
        }
    }

    @Override
    public void SendEmail(String toEmail, String password) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //邮件发件人
            helper.setFrom(myConfig.getSendUserName());
            //邮件收件人
            helper.setTo(toEmail);

            //邮件主题
            helper.setSubject("新的随机密码");
            //邮件内容
            helper.setText(String.format("你好，您的新的随机密码是：%s", password));
            //邮件发送时间
            helper.setSentDate(new Date());
            javaMailSender.send(message);
        } catch (Exception e) {
            throw new EmpExistException("邮件发送失败"+e.getMessage());
        }
    }
    @Override
    public String UpdatePassword(String newPassword) {
        System.out.println("@@@@@@@@"+newPassword);
        Integer empId = EmpUtil.getLoginEmpId();
        System.out.println("+++++++++"+empId);
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",empId);
        Emp emp = empMapper.selectOne(queryWrapper);
        if (emp == null){
            System.out.println("用户不存在");
            throw new EmpExistException(7000,"用户不存在");
        }else {
            //更新密码
            emp.setPassword(newPassword);
            System.out.println("********"+emp.getPassword());
            empMapper.updatePasswordByEmail(emp.getEmail(), emp.getPassword());
            return emp.getPassword();
        }
    }
}
